package com.mftour.moudles.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mftour.common.utils.MyDateUtils;
import com.mftour.common.utils.StringUtil;
import com.mftour.moudles.entity.AccountBook;
import com.mftour.moudles.mapper.AccountBookMapper;
import com.mftour.moudles.service.AccountBookService;

@Service
public class AccountBookServiceImpl extends BaseServiceImpl<AccountBook, Integer> implements AccountBookService {
	@Autowired
	AccountBookMapper accountBookMapper;

	@Override
	public boolean parseAccountBookExcel(File excelFile) {
		boolean result = false;
		List<AccountBook> accountBookList = new ArrayList<AccountBook>();
		int rowNum = 1, cellNum = 0;
		try {
			InputStream inp = new FileInputStream(excelFile);
			Workbook wb = WorkbookFactory.create(inp);
			Sheet sheet = wb.getSheetAt(0);

			for (Row row : sheet) {
				rowNum = row.getRowNum();
				if (rowNum > 1) {
					AccountBook accountBook = new AccountBook();
					if (StringUtil.convertStringToInteger(row.getCell(0).toString()) != null) {
						accountBook.setContractNum(StringUtil.convertStringToInteger(row.getCell(0).toString()));
						accountBook.setReciveTime(MyDateUtils.parseDate(row.getCell(1).toString()));
						accountBook.setContractTime(MyDateUtils.parseDate(row.getCell(2).toString()));
						accountBook.setPickupTime(MyDateUtils.parseDate(row.getCell(3).toString()));
						accountBook.setBuyOrg(row.getCell(4).toString());
						String tmp = row.getCell(5).toString();
						if (tmp != null) {
							String[] tmps = tmp.split("/");
							accountBook.setCarLevel(tmps[0]);
							accountBook.setSeatNum(
									StringUtil.convertStringToInteger(tmps[1].replace("座", "").replace("(LNG)", "")));
						}
						accountBook.setOperateLine(row.getCell(6).toString());
						accountBook.setSaleCompany(row.getCell(7).toString());
						accountBook.setFuelType(row.getCell(8).toString());

						accountBook.setCarType(row.getCell(9).toString());
						accountBook.setNum(StringUtil.convertStringToInteger(row.getCell(10).toString()));
						accountBook.setSaleUnitPrice(StringUtil.convertStringToLong(row.getCell(11).toString()));
						accountBook.setSalePrice(StringUtil.convertStringToLong(row.getCell(12).toString()));
						accountBook.setBuyUnitPrice(StringUtil.convertStringToLong(row.getCell(13).toString()));
						accountBook.setBuyPrice(StringUtil.convertStringToLong(row.getCell(14).toString()));
						accountBook.setGrossProfit(StringUtil.convertStringToLong(row.getCell(15).toString()));
						accountBook.setDeposit(StringUtil.convertStringToLong(row.getCell(16).toString()));
						accountBook.setDepositPayTime(MyDateUtils.parseDate(row.getCell(17).toString()));
						accountBook.setReceivables(row.getCell(18).toString());
						accountBook.setPayment(row.getCell(19).toString());

						accountBook.setCdPrice(StringUtil.convertStringToLong(row.getCell(20).toString()));
						accountBook.setTicketInto(row.getCell(21).toString());
						accountBook.setTicketOut(row.getCell(22).toString());

						accountBookList.add(accountBook);
					}
				}
			}
		} catch (Exception e) {
			System.out.println("error row num=" + (rowNum + 1));
			e.printStackTrace();
			return false;
		}
		System.out.println("parse size=" + accountBookList.size());
		if (accountBookList.size() > 0) {
			insertList(accountBookList);
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	public static void main(String[] args) {
		System.out.println(StringUtil.convertStringToInteger(""));
		System.out.println(new Integer(10) == 10);
	}
}